package com.tools.web.interceptor.auth.jwt;

import com.auth0.jwt.algorithms.Algorithm;
import com.tools.common.object.Note;
import com.tools.web.interceptor.auth.AuthSource;
import com.tools.web.interceptor.auth.AuthToken;
import com.tools.web.interceptor.auth.AuthUserService;
import com.tools.web.interceptor.auth._common.LocalMultipleAuthenticationManager;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Objects;
import java.util.concurrent.TimeUnit;

/**
 * 使用【JWT + 自定义本地缓存】 实现的 “用户多在线记录” 的 AuthenticationManager 身份认证拦截器
 * */
@Note("使用【JWT + 自定义本地缓存】 实现的 “用户多在线记录” 的 AuthenticationManager 身份认证拦截器")
public final class JWTLocalMultipleAuthenticationManager extends LocalMultipleAuthenticationManager {

    @Note("JWT 实现的 AuthToken 用于加密解密的工具")
    private final Algorithm authTokenAlgorithm;

    @Note("日志输出对象")
    private static final Logger LOGGER = LoggerFactory.getLogger(JWTLocalMultipleAuthenticationManager.class);

    /* **************************************************************************************
     *
     *          构造器
     *
     * **************************************************************************************
     * */

    public JWTLocalMultipleAuthenticationManager(AuthSource source, Algorithm authTokenAlgorithm) {
        this(1, TimeUnit.HOURS, source, null, authTokenAlgorithm);
    }

    public JWTLocalMultipleAuthenticationManager(AuthSource source, AuthUserService authUserService,
                                                 Algorithm authTokenAlgorithm) {
        this(1, TimeUnit.HOURS, source, authUserService, authTokenAlgorithm);
    }

    public JWTLocalMultipleAuthenticationManager(long execTime, TimeUnit execTimeUnit,
                                                 AuthSource source, Algorithm authTokenAlgorithm) {
        this(execTime, execTimeUnit, source, null, authTokenAlgorithm);
    }

    public JWTLocalMultipleAuthenticationManager(long execTime, TimeUnit execTimeUnit, AuthSource source,
                                                 AuthUserService authUserService, Algorithm authTokenAlgorithm) {
        super(execTime, execTimeUnit, source, authUserService);
        this.authTokenAlgorithm = Objects.requireNonNull(authTokenAlgorithm, "JWT 实现的 AuthToken 用于加密解密的工具 Algorithm 实例不能为 null");
    }

    /* **************************************************************************************
     *
     *          实现来自接口或父类的抽象方法
     *
     * **************************************************************************************
     * */

    @Override
    protected Logger protected_getLogger() {
        return LOGGER;
    }

    @Override
    protected String protected_getInstanceName() {
        return Integer.toHexString(this.hashCode());
    }

    @Override
    public AuthToken decryptAuthToken(String encryptedToken) {
        return new JWTAuthToken(this.authTokenAlgorithm, encryptedToken);
    }

    @Override
    public AuthToken newEmptyAuthToken() {
        return new JWTAuthToken(this.authTokenAlgorithm);
    }

    public Algorithm getAuthTokenAlgorithm() {
        return authTokenAlgorithm;
    }
}
